package com.example.pomelo_home.service.impl;

import com.example.pomelo_home.dao.ActivityRepository;
import com.example.pomelo_home.dto.ActivityDTO;
import com.example.pomelo_home.dto.ActivityTypeOrderCountDto;
import com.example.pomelo_home.entity.Activity;
import com.example.pomelo_home.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    ActivityRepository activityRepository;


    @Override
    public List<ActivityTypeOrderCountDto> getCountByActivityType() {
        List<Object[]> results = activityRepository.countOrdersByAllActivityTypes();
        List<ActivityTypeOrderCountDto> dtoList = new ArrayList<>();
        for (Object[] result : results) {
            String activityType = (String) result[0];
            Long orderCount = (Long) result[1];
            dtoList.add(new ActivityTypeOrderCountDto(activityType, orderCount));
        }
        return dtoList;
    }

    @Override
    public Page<Activity> findAllByPaged(Integer page, Integer size) {
        Pageable pageable= PageRequest.of(page-1,size);
        Page<Activity> all=activityRepository.findAll(pageable);
        return all;
    }

    @Override
    public Page<Activity> findByTitleLike(ActivityDTO activityDTO) {
        Pageable pageable= PageRequest.of(activityDTO.getPage()-1,activityDTO.getSize());
        Page<Activity> all=activityRepository.findByTitleLike("%"+activityDTO.getTitle()+"%",pageable);
        return all;
    }

    @Override
    public Boolean addActivity(ActivityDTO activityDTO) {
        Activity activity=new Activity(activityDTO.getTitle(),activityDTO.getPrice(),activityDTO.getStartTime(),activityDTO.getEndTime(),activityDTO.getAddress(),activityDTO.getPosterUrl(),activityDTO.getActivityType(),activityDTO.getMaxParticipants(),activityDTO.getOrganizerId(),activityDTO.getStatus(),activityDTO.getDescription());
        try {
            activityRepository.save(activity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean updateActivity(ActivityDTO activityDTO) {
        Activity activity=new Activity(activityDTO.getActivityId(),activityDTO.getTitle(),activityDTO.getPrice(),activityDTO.getStartTime(),activityDTO.getEndTime(),activityDTO.getAddress(),activityDTO.getPosterUrl(),activityDTO.getActivityType(),activityDTO.getMaxParticipants(),activityDTO.getOrganizerId(),activityDTO.getStatus(),activityDTO.getDescription());
        try {
            activityRepository.save(activity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            activityRepository.deleteById(id);
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    public List<Activity> findAll() {
        return activityRepository.findAll();
    }

    @Override
    public Activity findActivity(Integer id) {
        return activityRepository.findActivity(id);
    }


}
